/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spreadsheet.containers;

import com.eteks.jeks.JeksCell;
import com.eteks.jeks.JeksCellEditor;
import com.eteks.jeks.JeksTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import spreadsheet.spreadsheet.RowTable.RowHeaderTable;
import spreadsheet.spreadsheet.Table.Spreadsheet;
import spreadsheet.storablespreadsheet.StorableDataFrame;

/**
 *
 * @author Nobody
 */
public class SpreadsheetScrollpane extends JScrollPane implements ISpreadsheetContainer {

    public StorableDataFrame exportAsStorableDataFrame() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    private SpreadsheetWithFormula progressTable;
    private RowHeaderTable rowHeaderTable;

    public SpreadsheetWithFormula getProgressTable() {
        return progressTable;
    }


    public Spreadsheet getSpreadsheet() {
        return progressTable.getSpreadsheet();
    }

    public SpreadsheetScrollpane() {
        prepareNewPanel();
        rowHeaderTable = new RowHeaderTable((JeksTable) progressTable.getSpreadsheet());
        setViewportView(progressTable.getSpreadsheet());
        setRowHeaderView(rowHeaderTable);

    }

    public JTextField getFormulaField() {
        return progressTable.getTextField();
    }

    @Override
    public void layout() {
        //This catch is to fix bug of BasicTableHeaderUI , which throws exception when another than
        //main thread is trying to modify layout of component
        try {
            super.layout();
        } catch (java.util.NoSuchElementException ex) {
        } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
        }
    }

    @Override
    protected void validateTree() {
        try {
            super.validateTree();
        } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
        } catch(java.util.NoSuchElementException ex){
            
        }
    }

    public void setEnableTable(boolean enabled) {
        getProgressTable().getSpreadsheet().setEnabled(enabled);
    }

    private void prepareNewPanel() {

        progressTable = new SpreadsheetWithFormula(new Spreadsheet(), new JTextField());
        progressTable.getTextField().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String text = progressTable.getTextField().getText();
                Spreadsheet spreadsheet = progressTable.getSpreadsheet();

                // TODO Find a better way than using cell editor
                if (spreadsheet.getCellEditor(0, 0) instanceof JeksCellEditor) {
                    int editedRow =spreadsheet.getSelectedRow();
                    Object value = ((JeksCellEditor) spreadsheet.getCellEditor(0, 0)).getModelValue(text, spreadsheet.getModel(),
                            new JeksCell(editedRow,
                            spreadsheet.convertColumnIndexToModel(spreadsheet.getSelectedColumn())));
                    if (value != null) {
                        spreadsheet.setValueAt(value, spreadsheet.getSelectedRow(), spreadsheet.getSelectedColumn());
                        spreadsheet.setRowSelectionInterval(editedRow + 1, editedRow + 1);
                    }
                }
            }
        });


    }
}
